<?php
/**
 * @user: thanatos <thanatos915@163.com>
 */

namespace frontend\controllers;

use Yii;
use yii\web\Controller;

class BaseController extends Controller
{
    public $wechatUser;

    /**
     * @param \yii\base\Action $action
     * @return bool|\Symfony\Component\HttpFoundation\RedirectResponse
     * @throws \yii\web\BadRequestHttpException
     * @author thanatos <thanatos915@163.com>
     */
    public function beforeAction($action)
    {
        if (parent::beforeAction($action)) {
            $oauth = Yii::$app->wechat->official->oauth;
            $user = Yii::$app->session->get('wechat_user');
            //TODO 测试
//            $user = json_decode(file_get_contents('1.txt'), true);
            if (empty($user)){
                Yii::$app->session->set('target_url', Yii::$app->request->url);
                return $oauth->redirect(Yii::$app->request->hostInfo .'/index.php?r=site/oauth-callback')->send();
            } else {
                //TODO 测试
//                file_put_contents('1.txt', json_encode($user));
                $this->wechatUser = $user;
                return true;
            }
        }
        return false;
    }

}